package com.jzo2o.foundations.service.impl;




import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author hzzz
 * @since 2025-01-09
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {


    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO reqDTO) {
         return PageHelperUtils.selectPage(reqDTO,()->baseMapper.queryServeListByRegionId(reqDTO.getRegionId()));

    }

    @Override
    @Transactional()
    public void batchInsert(List<ServeUpsertReqDTO> serveList) {
        // 1、判空
        if (ObjectUtils.isEmpty(serveList)) {return;}

        for (ServeUpsertReqDTO serve : serveList) {
            // 2、 判断服务是否存在
            ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
            // 不存在或者存在但并未启用
            if (ObjectUtils.isEmpty(serveItem)||serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务未启用或服务不存在！");
            }
            // 3、判断该服务在该区域是否存在
            Integer count = lambdaQuery()
                    .eq(Serve::getServeItemId, serve.getServeItemId())
                    .eq(Serve::getRegionId, serve.getRegionId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException("该区域下该服务已经存在!");
            }
            // 4、插入数据
            Serve serve1 = BeanUtils.copyProperties(serve, Serve.class);
            Region region = regionMapper.selectById(serve1.getRegionId());
            serve1.setCityCode(region.getCityCode());
            save(serve1);
        }

    }

    @Override
    public void updatePriceById(Long id, BigDecimal price) {
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!update) {
            throw new CommonException("区域服务价格修改失败");
        }
    }

    @Override
    @Transactional
    public void onSale(Long id) {
        Serve serve = getById(id);
        if (serve == null) {
            throw new CommonException("该服务不存在！");
        }
        if ((serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()) && (serve.getSaleStatus() != FoundationStatusEnum.DISABLE.getStatus())) {
            throw new CommonException("该区域服务无法启用");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (ObjectUtils.isEmpty(serveItem)) {
            throw new CommonException("该服务项不存在！");

        }
        if (!(serveItem.getActiveStatus() == FoundationStatusEnum.ENABLE.getStatus())) {
            throw new CommonException("该服务项为启用，请先启用该服务项！");
        }

        boolean update = lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id)
                .update();
        if (!update) {
            throw new CommonException("服务上架失败！");
        }

    }

    @Override
    @Transactional
    public void deleteServe(Long id) {
        Serve serve = getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new CommonException("改区域服务不存在");
        }

        if (serve.getSaleStatus()!=FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("服务只有在草稿状态才能被删除！");
        }

        boolean rm = removeById(id);
        if (!rm) {
            throw new CommonException("服务删除失败！");
        }

    }

    @Override
    @Transactional
    public void offSale(Long id) {

        Serve serve = getById(id);
        if (serve == null||serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务不存在或服务状态不为上架！");
        }

        boolean update = lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .eq(Serve::getId, id)
                .update();
        if (!update) {
            throw new CommonException("下架失败！");
        }
    }

    @Override
    public void onHot(Long id) {
        Serve serve = getById(id);
        if (serve == null) {
            throw new CommonException("服务不存在");
        }

        if (serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new CommonException("服务未上架");
        }


        if (serve.getIsHot()!=0){
            throw new CommonException("服务状态为非热门才可设置！");
        }

        boolean update = lambdaUpdate()
                .set(Serve::getIsHot, 1)
                .eq(Serve::getId, id)
                .update();
        if (!update) {
            throw new CommonException("设置失败！");
        }
    }

    @Override
    public void offHot(Long id) {
        Serve serve = getById(id);
        if (serve == null) {
            throw new CommonException("服务不存在");
        }

        if (serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new CommonException("服务未上架");
        }


        if (serve.getIsHot()!=1){
            throw new CommonException("服务状态为热门才可取消！");
        }

        boolean update = lambdaUpdate()
                .set(Serve::getIsHot, 0)
                .eq(Serve::getId, id)
                .update();
        if (!update) {
            throw new CommonException("设置失败！");
        }
    }


}
